﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using ST.Buz.Base.Entity;
using ST.Buz.Base.Enums;
using ST.Buz.Base.ViewModel;
using ST.Buz.Service;
using ST.Common.Util;
using ST.Common.Util.Model;
using ST.Common.Util.Mvc;
using ST.Common.Util.Text;
using System;
using System.Net;

namespace ST.Buz.Web.Controllers
{
	[Route("sys/tablelog")]
	public class Sys_TableLogController : MyBaseController
	{
		private readonly IServiceSysTableLog service;
		private readonly IServiceSysAttachment serviceSysAttachment;
		private VM_RelatedModule[] relatedModules;

		public Sys_TableLogController(IServiceSysTableLog _service,
			IServiceSysAttachment _serviceSysAttachment,
			ILogger<Sys_TableLogController> _logger
		) : base(_logger)
		{
			service = _service;

			serviceSysAttachment = _serviceSysAttachment;
			relatedModules = new VM_RelatedModule[]{
				new VM_RelatedModule(){
					id="sys_attachment",
					name="相关附件",
					count=0
				}
			};
		}

		private VMS_Sys_TableLog GetSearchModelFromJson(JObject paras)
		{
			VMS_Sys_TableLog vm = new VMS_Sys_TableLog();
			vm.pnum = TextHelper.GetInt(paras, "pnum");
			vm.psize = TextHelper.GetInt(paras, "psize");
			vm.sortorder = TextHelper.GetString(paras, "sortorder");

			vm.infoid = TextHelper.GetInt(paras, "infoid");
			vm.tblid = TextHelper.GetInt(paras, "tblid");
			vm.content = TextHelper.GetString(paras, "content");
			vm.logman = TextHelper.GetString(paras, "logman");
			vm.logmanid = TextHelper.GetIntNullable(paras, "logmanid");
			vm.logmanip = TextHelper.GetString(paras, "logmanip");
			vm.date_s = TextHelper.GetDateTimeStart(paras, "date_s");
			vm.date_e = TextHelper.GetDateTimeEnd(paras, "date_e");
			vm.ids = TextHelper.GetInts(paras, "ids");
			return vm;
		}

		private VMS_Sys_TableLog GetSearchModelFromGetRequest()
		{
			VMS_Sys_TableLog vm = new VMS_Sys_TableLog();
			vm.pnum = TextHelper.GetInt(Request.Query["pnum"]);
			vm.psize = TextHelper.GetInt(Request.Query["psize"]);
			vm.sortorder = TextHelper.GetString(Request.Query["sortorder"]);

			vm.infoid = TextHelper.GetInt(Request.Query["infoid"]);
			vm.tblid = TextHelper.GetInt(Request.Query["tblid"]);
			vm.content = TextHelper.GetString(Request.Query["content"]);
			vm.logman = TextHelper.GetString(Request.Query["logman"]);
			vm.logmanid = TextHelper.GetIntNullable(Request.Query["logmanid"]);
			vm.logmanip = TextHelper.GetString(Request.Query["logmanip"]);
			vm.date_s = TextHelper.GetDateTimeStart(Request.Query["date_s"]);
			vm.date_e = TextHelper.GetDateTimeEnd(Request.Query["date_e"]);
			vm.ids = TextHelper.GetInts(Request.Query["ids"]);
			return vm;
		}

		private VMS_Sys_TableLog GetSearchModelFromPostRequest()
		{
			VMS_Sys_TableLog vm = new VMS_Sys_TableLog();
			vm.pnum = TextHelper.GetInt(Request.Form["pnum"]);
			vm.psize = TextHelper.GetInt(Request.Form["psize"]);
			vm.sortorder = TextHelper.GetString(Request.Form["sortorder"]);

			vm.infoid = TextHelper.GetInt(Request.Form["infoid"]);
			vm.tblid = TextHelper.GetInt(Request.Form["tblid"]);
			vm.content = TextHelper.GetString(Request.Form["content"]);
			vm.logman = TextHelper.GetString(Request.Form["logman"]);
			vm.logmanid = TextHelper.GetIntNullable(Request.Form["logmanid"]);
			vm.logmanip = TextHelper.GetString(Request.Form["logmanip"]);
			vm.date_s = TextHelper.GetDateTimeStart(Request.Form["date_s"]);
			vm.date_e = TextHelper.GetDateTimeEnd(Request.Form["date_e"]);
			vm.ids = TextHelper.GetInts(Request.Form["ids[]"]);
			return vm;
		}

		public IActionResult Index(string tid, string mid)
		{
			return RedirectToAction("List", new { tid, mid });
		}

		[ValidateSession]
		[HttpGet("list")]
		public IActionResult List(string tid, string mid)
		{
			int tableID = TextHelper.GetInt(tid);
			int infoID = TextHelper.GetInt(mid);
			ViewBag.TableID = tableID;
			ViewBag.InfoID = infoID;
			ViewBag.ServiceCode = "";
			//ViewBag.GenderOptions = GetEnums(typeof(Gender));
			return View();
		}

		[HttpPost("search")]
		public IActionResult Search([FromBody]JObject paras)
		{
			try
			{
				UserSession sessionCurrent = GetSessionFromRequest();
				var searchModel = GetSearchModelFromJson(paras);
				PageData pageData = service.GetPageList(searchModel);

				return new JsonResultPage(pageData);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "请求出错", (int)HttpStatusCode.InternalServerError);
			}
		}

		[HttpPost("export")]
		public IActionResult Export(JObject obj)
		{
			throw new NotImplementedException();
		}

		#region 辅助方法
		private string GetTableName(string tblid)
		{
			if (string.IsNullOrEmpty(tblid))
			{
				return string.Empty;
			}
			int tblID;
			if (int.TryParse(tblid, out tblID))
			{
				return Enum.GetName(typeof(TableCode), tblID);
			}
			return string.Empty;
		}

		private string GetTableName(int tblID)
		{
			return Enum.GetName(typeof(TableCode), tblID);
		}
		#endregion
	}
}